package com.uy.cuantoconocesdeuruguay.dialogs;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
import com.uy.cuantoconocesdeuruguay.FriendsRanking;
import com.uy.cuantoconocesdeuruguay.MainActivity;
import com.uy.cuantoconocesdeuruguay.MyChallenges;
import com.uy.cuantoconocesdeuruguay.R;
import com.uy.cuantoconocesdeuruguay.datatypes.FacebookUser;
import com.uy.cuantoconocesdeuruguay.datatypes.FriendChallengeEnvelope;
import com.uy.cuantoconocesdeuruguay.utiles.ImageLoader;
import com.uy.cuantoconocesdeuruguay.utiles.SoundsUtils;
import com.uy.cuantoconocesdeuruguay.utiles.StringPool;

public class ChallengeFinishedDialogUtils {
    private static final int RED = Color.rgb(230, 86, 64);
    private static final int GREEN = Color.rgb(97, 174, 36);
    private static final int GRAY = Color.rgb(184, 184, 184);
    private static final String CHALLENGE_FINISH_INTERSITIAL_MY_AD_UNIT_ID = "ca-app-pub-2103162040664326/3415243290";
    private Activity activity;	
	private InterstitialAd interstitial;
    
	public ChallengeFinishedDialogUtils(Activity activity) {
		this.activity = activity;
		loadIntersitial();
	}

	public void createChallengeFinishedPopup(final FriendChallengeEnvelope challenge, final boolean fromChallengesList){
		boolean victory = false;
		boolean proposedByMe = challenge.getProposerFacebookId().equals(MainActivity.userData.getFacebookid());
		if (proposedByMe){
			victory = challenge.getChallengeScoreProposer() > challenge.getChallengeScoreOponent();
		}else{
			victory = challenge.getChallengeScoreOponent() >= challenge.getChallengeScoreProposer();
		}
		View view = activity.getLayoutInflater().inflate(R.layout.challenge_finished, null);
		LayoutParams fullLayoutParams = new LayoutParams(activity.getResources().getDisplayMetrics().widthPixels,activity.getResources().getDisplayMetrics().heightPixels);
		view.setLayoutParams(fullLayoutParams);
		
		ImageView challengeFinishedIcon = (ImageView)view.findViewById(R.id.challengeFinishedIcon);
		TextView challengeFinishedText = (TextView)view.findViewById(R.id.challengeFinishedText);

		TextView challengeFinishedMyScore = (TextView)view.findViewById(R.id.challengeFinishedMyScore);
		TextView challengeFinishedOponentScore = (TextView)view.findViewById(R.id.challengeFinishedOponentScore);

		Button continueButton = (Button)view.findViewById(R.id.returnChallengesListButton);
		
		LinearLayout challengeFinishedMainContainer = (LinearLayout)view.findViewById(R.id.challengeFinishedMainContainer);
		if (!challenge.isAccepted()){
			challengeFinishedIcon.setImageResource(R.drawable.clock);
			challengeFinishedText.setText(activity.getString(R.string.my_challenges_view_adapter_time_up));
			challengeFinishedMyScore.setText(StringPool.BLANK);
			challengeFinishedOponentScore.setText(StringPool.BLANK);
			challengeFinishedMainContainer.setBackgroundColor(GRAY);
			((TextView)view.findViewById(R.id.challengeFinishedResult)).setText(StringPool.SPACE);
			continueButton.setTextColor(GRAY);
		}else{
			if (victory){
				challengeFinishedIcon.setImageResource(R.drawable.face_win);
				challengeFinishedText.setText(activity.getString(R.string.victory));
				challengeFinishedMainContainer.setBackgroundColor(GREEN);
				continueButton.setTextColor(GREEN);
			}else{
				challengeFinishedIcon.setImageResource(R.drawable.face_lose);
				challengeFinishedText.setText(activity.getString(R.string.defeat));
				challengeFinishedMainContainer.setBackgroundColor(RED);
				continueButton.setTextColor(RED);
			}
			if (proposedByMe){
				challengeFinishedMyScore.setText(String.valueOf(challenge.getChallengeScoreProposer()));
				challengeFinishedOponentScore.setText(String.valueOf(challenge.getChallengeScoreOponent()));
			}else{
				challengeFinishedOponentScore.setText(String.valueOf(challenge.getChallengeScoreProposer()));
				challengeFinishedMyScore.setText(String.valueOf(challenge.getChallengeScoreOponent()));
			}
		}

		ProgressBar myLoader = (ProgressBar)view.findViewById(R.id.challengeFinishedMyPictureLoader);
		ProgressBar friendLoader = (ProgressBar)view.findViewById(R.id.challengeFinishedOponentPictureLoader);

		ImageView challengeFinishedMyPicture = (ImageView)view.findViewById(R.id.challengeFinishedMyPicture);
		ImageView challengeFinishedOponentPicture = (ImageView)view.findViewById(R.id.challengeFinishedOponentPicture);
		ImageLoader.loadImageOn(activity, challengeFinishedMyPicture, MainActivity.userData.getFacebookImage(), myLoader);
		if (challenge.getOponent() == null){
			ImageLoader.loadImageOn(activity, challengeFinishedOponentPicture, R.drawable.com_facebook_profile_default_icon, friendLoader);
		}else{
			ImageLoader.loadImageOn(activity, challengeFinishedOponentPicture, challenge.getOponent().getPicture(), friendLoader);

			challengeFinishedOponentPicture.setOnClickListener(new OnClickListener() {			
				@Override
				public void onClick(View arg0) {	
					FacebookUser friend = FriendsRanking.getFriend(activity, challenge.getOponent().getId());					
					FriendProfileDialogUtils dialog = new FriendProfileDialogUtils(activity);
					dialog.createProfilePopup(friend);
				}
			});
		}
			
		challengeFinishedMyPicture.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View arg0) {				
				FacebookUser friend = FriendsRanking.getFriend(activity, MainActivity.userData.getFacebookid());
				FriendProfileDialogUtils dialog = new FriendProfileDialogUtils(activity);
				dialog.createProfilePopup(friend);
			}
		});
		
		AlertDialog.Builder builder = new AlertDialog.Builder(activity);
	    builder.setView(view);
	    builder.setCancelable(true);
	    final Dialog challengeFinishedPopup = builder.create();
	    challengeFinishedPopup.setOnDismissListener(new OnDismissListener() {			
			@Override
			public void onDismiss(DialogInterface arg0) {
				if (!fromChallengesList){
					if (MainActivity.userData.shuldShowChallengeAd()){
						displayInterstitial();
					}
				}
			}
		});
		challengeFinishedPopup.show();	 
		if (!fromChallengesList){
			if (challenge.isAccepted()){
				if (victory){
					SoundsUtils.playSound(activity, R.raw.game_win);
				}else{
					SoundsUtils.playSound(activity, R.raw.game_lose);
				}
			}else{
				SoundsUtils.playSound(activity, R.raw.ticking);
			}
		}
		
		continueButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (!fromChallengesList){
					activity.startActivity(new Intent(activity.getApplicationContext(), MyChallenges.class));
					activity.finish();
				}
				challengeFinishedPopup.dismiss();
			}
		});
	}
	private void loadIntersitial() {
		interstitial = new InterstitialAd(activity);
	    interstitial.setAdUnitId(CHALLENGE_FINISH_INTERSITIAL_MY_AD_UNIT_ID );
	    AdRequest adRequest = new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();
	    interstitial.loadAd(adRequest);
	}

	private void displayInterstitial() {
		if (interstitial.isLoaded()) {
			interstitial.show();
		}
	}
}
